var id;
//添加newId自定义事件监听
window.addEventListener('pdetailId',function(event){
	// 获得事件参数
	id = event.detail.id;
	$.ajax({
		url:backend_url + "/is/gi.action",
		data:{
			"id":id
		},
		type:"POST",
		dataType:"json",
		success:function(data){
			$("#content_area").empty();
			// console.log(data);
			//reserved1：用户名，reserved2：用户头像
			var issueTypeid = data.b.issueTypeid;
			var issueType = judge_issueType(issueTypeid);
			var imgLocation = data.b.imageLocation;
			var images = "";
			if(imgLocation != null && imgLocation.length != 0){
				images = imgLocation.split(";");
			}
			var div = `<div class="content-area">
						<h3 class="issuetitle">`+data.b.issueTitle+`</h3>
						<div>
							<div id="head_div" sendId=`+data.b.userId+` >
								<img class="head" src=`+img_url+data.b.reserved2+` onerror="this.onerror='';src='../images/error_img.png' " alt="头像">
							</div>
							<span class="name">`+data.b.reserved1+`</span>
							<span class="mui-pull-right type">|`+data.b.dealTime+`]</span>
							<span class="mui-pull-right type">[`+issueType+`|</span>
						</div>
						<p class="content">`+data.b.issueDescribe+`</p>`;
			if(images.length>1){
				images.pop();
				for(var i =0 ; i< images.length;i++){
					// console.log(images[i].substring(2));
					var img = `<img class="pro_img" src=`+img_url+images[i]+` onerror="this.onerror='';src='../images/error_img.png' " alt="产品图片">`;
					div += img;
				}
			}else if(images != ""){
				// 单张图
				div += `<img class="pro_img" src=`+img_url+images+` onerror="this.onerror='';src='../images/error_img.png' " alt="产品图片">`;
			}
			div+=`</div>`;
			$("#content_area").append(div);
			//点击头像判断是否登录，已登录可聊天
			$("#head_div").on("click",function(){
				goChat(data.b.userId,data.b.reserved1,data.b.reserved2);//发布商品的人的id
			});
		},
		error:function(er){
			// console.log(er);
			alert("呃。。网络似乎除了什么问题，请检查网络连接或重试！");
			//window.location.reload();
		}
	});
});
// 通过issueTypeid判断issueType
function judge_issueType(issueTypeid){
	var issueType;
	if(issueTypeid == 1){
		issueType = "金属制品";
	}else if(issueTypeid == 2){
		issueType = "纸制品";
	}else if(issueTypeid == 3){
		issueType = "电子产品";
	}else if(issueTypeid == 4){
		issueType = "塑胶制品";
	}else{
		issueType = "木制品";
	}
	return issueType;
}
function goChat(sendId,tosendname,ToSendHead){
	var userId;//当前登录用户
	mui.plusReady(function() {
		id = plus.navigator.getCookie( "id");
		// userId为null,类型为object
		if(typeof(id) == "string"){
			userId = id;
			// alert(sendId+userId);
			if(sendId == userId){
				var ifcenter = confirm("哎哟，居然逛到了自己发的商品了，是否前去个人中心？");
				if(ifcenter){
					plus.webview.open('./center.html', 'new', {}, 'slide-in-right', 200);
				}else{
					return false;
				}
			}else{
				var ifchat = confirm("要和TA聊聊吗？");
				if(ifchat){
					mui.init({
						preloadPages:[{
							id:'message',
							url:'./message.html'
						}]
					});
					messagePage = plus.webview.getWebviewById('message');
					mui.fire(messagePage,'messageId',{
						tosendId:sendId,
						userId:userId,
						tosendname:tosendname,
						ToSendHead:ToSendHead,
					});
					mui.openWindow({
						id:"message"
					});
					// plus.webview.open('./message.html', 'new', {}, 'slide-in-right', 200);
				}else{
					return false;
				}
			}
		}else{
			var iflogin = confirm("暂未登录，是否前去登录？");
			if(iflogin){
				plus.webview.open('./before-login.html', 'new', {}, 'slide-in-right', 200);
			}else{
				return false;
			}
		}
	});	
}
